package com.lmz.project.redis.interview;

public class ListNodeTest {
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val,ListNode next) {
            this.val = val;
            this.next = next;
        }



        //迭代
        public static ListNode iterate(ListNode head) {
            ListNode next,prev = null;
            ListNode curr = head;
            while (curr !=null){
                //记录当前节点的下一个节点的值
                next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next; //下一次循环使用
            }

            return prev;
        }


        public static ListNode recursion(ListNode head){
            if (head == null || head.next == null){
                return head;
            }
            ListNode newHead = recursion(head.next);
            //算法内容
            head.next.next = head;
            head.next = null;
            return newHead;
        }

        public static void main(String[] args) {
            ListNode node5 = new ListNode(5, null);
            ListNode node4 = new ListNode(4, node5);
            ListNode node3 = new ListNode(3, node4);
            ListNode node2 = new ListNode(2, node3);
            ListNode node1 = new ListNode(1, node2);
//            ListNode iterate = iterate(node1);
            ListNode iterate = recursion(node1);
            System.out.println(iterate);
        }

    }
}
